package com.hstcs.update;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.badlogic.gdx.files.FileHandle;
import com.hstcs.game.XGame;

public class UpdateManager{
	public static UpdateManager instance = null;
	public static UpdateManager getInstance(){
		if(instance == null){
			instance = new UpdateManager();
		}
		return instance;
	}
	
	public static boolean updateComplete = false;
	public static String urlHead = "http://192.168.1.217/bin/dx.apk";
//	public static boolean loadCodeFinish = false;
	
	 
	public void updateCode()
	{
		new Thread(new Runnable() {
			@Override
			public void run() {
				loadData(urlHead, new HandleCode());
			}
		}).start();
	}

	public void loadData(String url, HttpResponseListener listener){
		HttpRequest httpRequest = new HttpRequest(Net.HttpMethods.GET);
		httpRequest.setUrl(url);
		Gdx.net.sendHttpRequest(httpRequest, listener);			
	}
	
	public boolean update(){
		return updateComplete;
	}
	
	public Class  loadCode()
	{
		FileHandle file = Gdx.files.local("template/dx.apk");
		try {
			return XGame.getInstance().loader.load(file.file(),Gdx.files.getLocalStoragePath(),  "com.hstcs.screen.MainMenuScreen");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return null;
	}
}
